import money from '@/api/money'

const state = {
    sign: 0,
    wukong: 0,
    month_total: 0,
    percentage: 0,
    remain: 0,
    return: 0,
    share: 0,
    total: 0,
    revenueHistory: {
        data: [],
        noData: false,
        loading: false,
        allLoaded: false
    },
    revenueMonthly: {
        data: [],
        noData: false,
        loading: false,
        allLoaded: false
    },
    lastMonth: {
        flag: 0
    },
    requestSend: {
        getMoneyOverview: false,
        getLastMonth: false
    }
}

const getters = {
    moneyData: state => state,
    thisMonth: () => new Date().getMonth() + 1,
    lastMonth: state => state.lastMonth,
    revenueHistory: state => state.revenueHistory,
    revenueMonthly: state => state.revenueMonthly
}

const actions = {
    async getMoneyOverview ({state, commit}) {
        if (state.requestSend.getMoneyOverview) return
        console.log(money.getIncome2);
        const {data: data1} = await money.getIncome()
        const {data: data2} = await money.getIncome2()
        commit('moneyOverview', {data1, data2})
        commit('requestComplete', 'getMoneyOverview')
    },
    async getLastMonth ({state, commit}) {
        if (state.requestSend.getLastMonth) return state.lastMonth
        const {data, success} = await money.getLastMonth()
        commit('moneyLastMonth', data)
        commit('requestComplete', 'moneyLastMonth')
    },
    async drawMoney ({commit}, {amount, datang}) {
        const {data, success, msg} = await money.drawMoney({amount, datang})
        if (success) commit('recalculateMoney', amount)
        return {success, msg}
    },
    // 历史收益明细
    async getRevenueHistory ({commit}, {type, page}) {
        commit('multiplePageRequest', {name: 'revenueHistory', ajaxing: true})
        const {data, success} = await money.getRevenueHistory({type, page})
        commit('revenueHistory', data)
        commit('multiplePageRequest', {name: 'revenueHistory', ajaxing: false})
        return success
    },
    // 月收益列表
    async getRevenueMonthly ({commit}, {page}) {
        commit('multiplePageRequest', {name: 'revenueMonthly', ajaxing: true})
        const {data, success} = await money.getRevenueMonthly({page})
        commit('revenueMonthly', data)
        commit('multiplePageRequest', {name: 'revenueMonthly', ajaxing: false})
        return success
    }
}

const mutations = {
    requestComplete (state, item) {
        state.requestSend[item] = true
    },
    multiplePageRequest (state, {name, ajaxing}) {
        state[name].loading = ajaxing
    },
    moneyOverview(state, {data1, data2}){
		state.month_total = data1.month_total
		state.percentage = data1.percentage
		state.remain = data1.remain
		state.return = data1.return
		state.share = data1.share
		state.total = data1.total
        state.sign = data2.point1
        state.wukong = data2.point2
        state.point1 = data2.point1//签到积分
        state.point2 = data2.point2//提现积分
        state.point3 = data2.point3//购物积分
	},
    moneyLastMonth (state, data) {
        state.lastMonth = data
    },
    recalculateMoney (state, amount) {
        state.remain = state.remain - amount
    },
    revenueHistory (state, data) {
        const dataWithMonth = data.map(d => ({
            month: new Date().getMonth(d.created_at) + 1,
            year: new Date().getFullYear(d.created_at),
            ...d
        })
        )
        const rev = state.revenueHistory
        if (!data.length) {
            if (!rev.data.length) rev.noData = true
            else rev.allLoaded = true
        }
        rev.data = [...rev.data, ...dataWithMonth]
    },
    clearRevenueHistory (state) {
        const rev = state.revenueHistory
        rev.data = []
        rev.noData = false
        rev.loading = false
        rev.allLoaded = false
    },
    revenueMonthly (state, data) {
        const rev = state.revenueMonthly
        if (!data.length) {
            if (!rev.data.length) rev.noData = true
            else rev.allLoaded = true
        }
        rev.data = data
    }
}

export default {
    state,
    getters,
    actions,
    mutations
}
